<template>
  <a-card :body-style="{ padding: 0 }">
    <div>
      <a-button key="1" type="primary" @click="handleAdd" style="margin-right: 10px;" :disabled="addbuttonstate">
        新增
      </a-button>
      <a-dropdown style="margin-right: 10px;" :disabled="referbuttonstate">
        <a-menu slot="overlay" @click="hanleRefer">
          <a-menu-item key="1">参照采购计划 </a-menu-item>
          <a-menu-item key="2">参照销售订单</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 参照 <a-icon type="down" /> </a-button>
      </a-dropdown>
      <a-button key="2" type="primary" @click="handleAdd" style="margin-right: 10px;" :disabled="canclebuttonstate">
        取消
      </a-button>
      <a-button key="3" type="primary" @click="handleOk" :disabled="savebuttonstate" style="margin-right: 10px;">
        保存
      </a-button>
      <a-button key="4" type="primary" @click="handleAddSubmit" :disabled="submitbuttonstate"
        style="margin-right: 10px;">
        提交
      </a-button>
      <a-button key="5" type="primary" @click="handleDel" :disabled="deletebuttonstate" style="margin-right: 10px;">
        删除
      </a-button>
      <a-button key="6" type="primary" @click="handleDel" :disabled="changebuttonstate" style="margin-right: 10px;">
        变更
      </a-button>
      <a-button key="7" type="primary" @click="handleEdit" :disabled="editbuttonstate" style="margin-right: 10px;">
        编辑
      </a-button>
      <a-dropdown style="margin-right: 10px;" :disabled="printbuttonstate">
        <a-menu slot="overlay" @click="handlePrintWithModelId">
          <a-menu-item key="po">打印Pdf </a-menu-item>
          <a-menu-item key="2">excel导出</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 打印 <a-icon type="down" /> </a-button>
      </a-dropdown>
      <a-dropdown style="margin-right: 10px;" :disabled="auditbuttonstate">
        <a-menu slot="overlay" @click="handleAddPost">
          <a-menu-item key="2">审批通过 </a-menu-item>
          <a-menu-item key="3">审批不通过</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 审批 <a-icon type="down" /> </a-button>
      </a-dropdown>
    </div>
    <a-spin :spinning="confirmLoading">
      <j-form-container :disabled="formDisabled">
        <!-- 主表单区域 -->
        <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
          <a-row>
            <a-col :span="6">
              <a-form-model-item label="费用编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="billcode">
                <a-input v-model="model.billcode" placeholder="请输入费用编号"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="6">
              <a-form-model-item label="费用日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="billdate">
                <j-date placeholder="请选择费用日期" v-model="model.billdate" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <!-- <a-col :span="6" >
            <a-form-model-item label="公司" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="companyid">
              <a-input v-model="model.companyid" placeholder="请输入公司" ></a-input>
            </a-form-model-item>
          </a-col> -->
            <a-col :span="6">
              <a-form-model-item label="项目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectid">
                <!-- <a-input v-model="model.projectid" placeholder="请输入项目"></a-input> -->
                <j-search-select-tag v-model="model.projectid" dict="project,name,id"></j-search-select-tag>
              </a-form-model-item>
            </a-col>
            <a-col :span="6">
              <a-form-model-item label="任务" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskid">
                <!-- <a-input v-model="model.taskid" placeholder="请输入任务"></a-input> -->
                <j-search-select-tag v-model="model.taskid" dict="projecttask,subject,id"></j-search-select-tag>
              </a-form-model-item>
            </a-col>
            <a-col :span="6">
              <a-form-model-item label="费用发生人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="assigntoid">
                <!-- <a-input v-model="model.taskid" placeholder="请输入任务"></a-input> -->
                <!-- <c-select-user v-model="model.assigntoid" :buttons="false"></c-select-user> -->
                <j-select-multi-user v-model="model.assigntoid"></j-select-multi-user>
              </a-form-model-item>
            </a-col>
            <a-col :span="6">
              <a-form-model-item label="总含税金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totaltaxinvalue">
                <a-input-number v-model="model.totaltaxinvalue" placeholder="请输入总含税金额" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="6">
              <a-form-model-item label="总不含税金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totaltaxexvalue">
                <a-input-number v-model="model.totaltaxexvalue" placeholder="请输入总不含税金额" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="6">
              <a-form-model-item label="税额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totaltax">
                <a-input-number v-model="model.totaltax" placeholder="请输入税额" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="6">
              <a-form-model-item label="币种" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="currencyid">
                <!-- <a-input v-model="model.currencyid" placeholder="请输入币种"></a-input> -->
                <j-search-select-tag v-model="model.currencyid" dict="currency,currencyname,id"></j-search-select-tag>
              </a-form-model-item>
            </a-col>
            <a-col :span="6">
              <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remarks">
                <a-input v-model="model.remarks" placeholder="请输入备注"></a-input>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </j-form-container>
      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="项目费用明细" :key="refKeys[0]" :forceRender="true">
          <j-vxe-table keep-source :ref="refKeys[0]" :loading="projectexpenseinfoitemTable.loading"
            :columns="projectexpenseinfoitemTable.columns" :dataSource="projectexpenseinfoitemTable.dataSource"
            :maxHeight="300" :disabled="formDisabled" :rowNumber="true" :rowSelection="true"
            @2valueChange="handle2valuechange" :toolbar="true" />
        </a-tab-pane>
      </a-tabs>
    </a-spin>
  </a-card>

</template>

<script>

import { getAction } from '@/api/manage'
import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
import { JVXETypes } from '@/components/jeecg/JVxeTable'
import { getRefPromise, VALIDATE_FAILED } from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
import { validateDuplicateValue } from '@/utils/util'
import JFormContainer from '@/components/jeecg/JFormContainer'

export default {
  name: 'ProjectexpenseinfoForm',
  mixins: [JVxeTableModelMixin],
  components: {
    JFormContainer,
  },
  data () {
    return {
      objectcode: "projectexpense",
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      model: {
      },
      // 新增时子表默认添加几行空数据
      addDefaultRowNum: 1,
      validatorRules: {
      },
      refKeys: ['projectexpenseinfoitem',],
      tableKeys: ['projectexpenseinfoitem',],
      activeKey: 'projectexpenseinfoitem',
      // 项目费用明细
      projectexpenseinfoitemTable: {
        loading: false,
        dataSource: [],
        columns: [
          {
            title: '费用类型',
            key: 'expensetype',
            type: JVXETypes.input,
            width: "200px",
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '费用含税金额',
            key: 'taxinvalue',
            type: JVXETypes.inputNumber,
            width: "200px",
            placeholder: '请输入${title}',
            defaultValue: '',
            statistics: ['sum'],
          },
          {
            title: '费用不含税金额',
            key: 'taxexvalue',
            type: JVXETypes.inputNumber,
            width: "200px",
            placeholder: '请输入${title}',
            defaultValue: '',
            statistics: ['sum'],
          },
          {
            title: '税额',
            key: 'tax',
            type: JVXETypes.inputNumber,
            width: "200px",
            placeholder: '请输入${title}',
            defaultValue: '',
            statistics: ['sum'],
          },
          {
            title: '备注',
            key: 'remarks',
            type: JVXETypes.input,
            width: "200px",
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '附件',
            key: 'attachid',
            type: JVXETypes.file,
            token: true,
            responseName: "message",
            width: "200px",
            placeholder: '请选择文件',
            defaultValue: '',
          },
          {
            title: 'parentid',
            key: 'parentid',
            type: JVXETypes.hidden,
            width: "200px",
            placeholder: '请输入${title}',
            defaultValue: '',
          },
        ]
      },
      url: {
        add: "/project.expenseinfo/projectexpenseinfo/add",
        edit: "/project.expenseinfo/projectexpenseinfo/edit",
        queryById: "/project.expenseinfo/projectexpenseinfo/queryById",
        projectexpenseinfoitem: {
          list: '/project.expenseinfo/projectexpenseinfo/queryProjectexpenseinfoitemByMainId'
        },
      }
    }
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  computed: {
    formDisabled () {
      return this.disabled
    },
  },
  created () {
  },
  methods: {
    handle2valuechange (event) {
      const { type, row, column, value, oldValue, target, cellTarget, columnIndex, rowIndex, isSetValues } = event
      if (column.key === 'taxinvalue') {
        if (value === '' || value === undefined) {
          return
        }

        row.tax = parseFloat(value - row.taxexvalue, 2);

        let xtable = target;
        let fullData = xtable.getTableData()
        let totaltaxinvalue = 0;
        for (let i = 0; i < fullData.length; i++) {
          totaltaxinvalue += fullData[i].taxinvalue;
        }
        // this.model.totaltaxinvalue = totaltaxinvalue;
        this.$set(this.model, "totaltaxinvalue", totaltaxinvalue);
      }
      if (column.key === 'taxexvalue') {
        if (value === '' || value === undefined) {
          return
        }

        row.tax = parseFloat(row.taxinvalue - value, 2);

        let xtable = target;
        let fullData = xtable.getTableData()
        let totaltaxinvalue = 0;
        for (let i = 0; i < fullData.length; i++) {
          totaltaxinvalue += fullData[i].taxexvalue;
        }
        // this.model.totaltaxinvalue = totaltaxinvalue;
        this.$set(this.model, "totaltaxexvalue", totaltaxinvalue);

      }
      this.$set(this.model, 'totaltax', parseFloat(this.model.totaltaxinvalue - this.model.totaltaxexvalue, 2))

    },
    addBefore () {
      this.projectexpenseinfoitemTable.dataSource = []
    },
    getAllTable () {
      let values = this.tableKeys.map(key => getRefPromise(this, key))
      return Promise.all(values)
    },
    /** 调用完edit()方法之后会自动调用此方法 */
    editAfter () {
      this.$nextTick(() => {
      })
      // 加载子表数据
      if (this.model.id) {
        let params = { id: this.model.id }
        this.requestSubTableData(this.url.projectexpenseinfoitem.list, params, this.projectexpenseinfoitemTable)
      }
    },
    //校验所有一对一子表表单
    validateSubForm (allValues) {
      return new Promise((resolve, reject) => {
        Promise.all([
        ]).then(() => {
          resolve(allValues)
        }).catch(e => {
          if (e.error === VALIDATE_FAILED) {
            // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
            this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
          } else {
            console.error(e)
          }
        })
      })
    },
    /** 整理成formData */
    classifyIntoFormData (allValues) {
      let main = Object.assign(this.model, allValues.formValue)
      return {
        ...main, // 展开
        projectexpenseinfoitemList: allValues.tablesValue[0].tableData,
      }
    },
    validateError (msg) {
      this.$message.error(msg)
    },

  }
}
</script>

<style scoped></style>